package cn.stylefeng.guns.sys.modular.system.db5.service.impl;

import cn.stylefeng.guns.sys.modular.system.db5.entity.PortalUser;
import cn.stylefeng.guns.sys.modular.system.db5.mapper.PortalUserMapper;
import cn.stylefeng.guns.sys.modular.system.db5.mapper.UserDataMapper;
import cn.stylefeng.guns.sys.modular.system.db5.model.result.MenDianInfoResult;
import cn.stylefeng.guns.sys.modular.system.db5.service.UserDataService;
import cn.stylefeng.guns.sys.modular.system.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserDataServiceImpl implements UserDataService {
    private Logger logger = LoggerFactory.getLogger(UserDataServiceImpl.class);

    @Autowired
    private UserDataMapper userDataMapper;
    @Resource
    private PortalUserMapper portalUserMapper;

    @Override
    public List<Integer> findAllShopsByMobile(String param) {

        List<Integer> allShopsByMobile = userDataMapper.findAllShopsByMobile(param);

        return allShopsByMobile;
    }

    @Override
    @Async
    public void changePwd(String account, String password) {
        PortalUser portalUser=findSalt(account);
        //根据手机号修改小妖密码
        String passwords= MD5Util.encrypt(password + portalUser.getSalt());
        portalUser.setPassword(passwords);
        portalUserMapper.updateById(portalUser);
    }

    @Override
    public List<MenDianInfoResult> findAllMenDianInfo() {
        List<MenDianInfoResult> menDianInfoResultList = userDataMapper.findAllMenDianInfo();
        return menDianInfoResultList;
    }

    public PortalUser findSalt(String account) {
        //根据手机号查出对应的盐
        LambdaQueryWrapper<PortalUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PortalUser::getLoginName,account);
        PortalUser portalUser= portalUserMapper.selectOne(queryWrapper);

        return portalUser;
    }

    @Override
    public void changeMobile(String oldTels,String mobile){
        //查询原手机号信息
        PortalUser portalUserOldTels = new LambdaQueryChainWrapper<>(portalUserMapper)
                .eq(PortalUser::getLoginName, oldTels)
                .one();
        if(portalUserOldTels!=null){
            PortalUser portalUserMobile = new LambdaQueryChainWrapper<>(portalUserMapper)
                    .eq(PortalUser::getLoginName, mobile)
                    .one();
            portalUserOldTels.setIsUse("PUB005001");
            portalUserOldTels.setIsUseView("是");
            String pub="PUB005001";
            //新手机号不存在库中 更新新手手机号并更新状态
            if(portalUserMobile==null&&!portalUserOldTels.getLoginName().equals(mobile)&&!oldTels.equals(mobile)){
                portalUserOldTels.setMobile(mobile);
                portalUserOldTels.setLoginName(mobile);
                portalUserOldTels.setOldTels(oldTels);
                portalUserMapper.updateById(portalUserOldTels);
            //新手机号库中已存在 更新新手机号并更新状态,更新原手机号状态
            }else if(portalUserMobile!= null&&!portalUserMobile.getLoginName().equals(oldTels)&&!oldTels.equals(mobile)){
                portalUserMobile.setIsUse("PUB005001");
                portalUserMobile.setIsUseView("是");
                portalUserMobile.setOldTels(oldTels);
                portalUserMapper.updateById(portalUserMobile);
                portalUserOldTels.setIsUse("PUB005002");
                portalUserOldTels.setIsUseView("否");
                portalUserMapper.updateById(portalUserOldTels);
            }else if(portalUserMobile!= null&&!portalUserMobile.getIsUse().equals(pub)){
                //新旧号码一致 判断状态是否是在使用，未使用改为使用状态
                portalUserMapper.updateById(portalUserOldTels);
            }
        }
    }
}
